package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.CompanyContact;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.CompanyContactService;
import com.cz.zslu.service.IBaseAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 联系方式 前端控制器
 * </p>
 *
 * @author Bruce
 * @since 2019-05-30
 */
@RestController
@RequestMapping("/sys/contact")
@Api(description = "联系方式管理")
public class CompanyContactController extends BaseController<CompanyContactService, CompanyContact> {


    @Autowired
    private CompanyContactService companyContactService;
    @Autowired
    private IBaseAreaService iBaseAreaService;


    @ApiOperation("通过storeId获取详情")
    @IgnoreAuth
    @RequestMapping(value = "/findInfoByStoreId", method = RequestMethod.POST)
    public ReturnObject<CompanyContact> findInfoByStoreId(@ApiParam("storeId") @RequestParam String storeId) {


        return new ReturnObject<>(0, "获取成功",
                companyContactService.selectOne(new EntityWrapper<CompanyContact>().eq("store_id", storeId)));
    }

    @ApiOperation("新增/编辑（新增时：id为空）")
    @RequestMapping(value = "/saveContact", method = RequestMethod.POST)
    public ReturnObject<CompanyContact> saveIntroduce(@ApiParam("企业名称") @RequestParam String companyName,
                                                      @ApiParam("电话") @RequestParam String telephone,
                                                      @ApiParam("省") Long provinceId,
                                                      @ApiParam("市") Long cityId,
                                                      @ApiParam("区") Long districtId,
                                                      @ApiParam("地址") @RequestParam String address,
                                                      @ApiParam("联系人") @RequestParam String linkMan,
                                                      @ApiParam("传真") @RequestParam String portraiture,
                                                      @ApiParam("网址") @RequestParam String companyNet,
                                                      @ApiParam("邮箱") @RequestParam String email,
                                                      @ApiParam("qq") @RequestParam String qq,
                                                      @ApiParam("商铺id") @RequestParam Long storeId,
                                                      @ApiParam("id") @RequestParam(defaultValue = "0") Long id) {

        if (id != 0) {
            CompanyContact info = companyContactService.selectById(id);
            info.setCompanyName(companyName);
            info.setTelephone(telephone);
            info.setProvinceId(provinceId);
            info.setDistrictId(districtId);
            info.setCityId(cityId);
            String area =
                    iBaseAreaService.selectById(provinceId).getName() + iBaseAreaService.selectById(cityId).getName() + iBaseAreaService.selectById(districtId).getName();
            info.setArea(area);
            info.setAddress(address);
            info.setLinkMan(linkMan);
            info.setPortraiture(portraiture);
            info.setCompanyNet(companyNet);
            info.setEmail(email);
            info.setQq(qq);
            baseService.updateById(info);
        } else {

            CompanyContact info = new CompanyContact();
            info.setCompanyName(companyName);
            info.setTelephone(telephone);
            info.setProvinceId(provinceId);
            info.setDistrictId(districtId);
            info.setCityId(cityId);
            String area =
                    iBaseAreaService.selectById(provinceId).getName() + iBaseAreaService.selectById(cityId).getName() + iBaseAreaService.selectById(districtId).getName();
            info.setArea(area);
            info.setAddress(address);
            info.setLinkMan(linkMan);
            info.setPortraiture(portraiture);
            info.setCompanyNet(companyNet);
            info.setEmail(email);
            info.setQq(qq);
            info.setStoreId(storeId);
            baseService.insert(info);
        }

        return new ReturnObject<>(0, "新增成功", null);
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/delById", method = RequestMethod.POST)
    public ReturnObject<CompanyContact> delById(@ApiParam("id") @RequestParam Long id) {

        CompanyContact info = baseService.selectById(id);
        info.setState(1);
        baseService.updateById(info);

        return new ReturnObject<>(0, "删除成功", null);
    }


}

